<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-date-picker
          v-model="dataForm.punchDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.username" placeholder="学生姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        prop="nickName"
        header-align="center"
        align="center"
        label="学生姓名">
      </el-table-column>
      <el-table-column
        prop="punchDate"
        header-align="center"
        align="center"
        label="打卡日期">
      </el-table-column>
      <el-table-column
        prop="punchTime"
        header-align="center"
        align="center"
        width="180px"
        label="打卡时间">
        <template slot-scope="scope">
          <el-tag
            :key="scope.row.punchTime"
            :type="scope.row.punchTime == null ? 'danger' :  'success'"
            effect="plain"
            size="small">{{scope.row.punchTime == null ? '暂无打卡纪录' : scope.row.punchTime}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="punchStatus"
        header-align="center"
        align="center"
        label="打卡状态">
        <template slot-scope="scope">
          <el-tag
            :key="scope.row.punchStatus"
            :type="scope.row.punchStatus == 0 ? 'danger' :  'success'"
            effect="plain"
            size="small">{{scope.row.punchStatus == 0 ? '未打卡' : '已打卡'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="isNormal"
        header-align="center"
        align="center"
        label="体温是否正常">
        <template slot-scope="scope">
          <el-tag
            :key="scope.row.isNormal"
            :type="scope.row.isNormal == 1 ? 'success' :  'danger'"
            effect="plain"
            size="small">{{scope.row.isNormal == 1 ? '体温正常' : '体温异常'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="isHot"
        header-align="center"
        align="center"
        label="是否出现发热">
        <template slot-scope="scope">
          <el-tag
            :key="scope.row.isHot"
            :type="scope.row.isHot == 0 ? 'success' :  'danger'"
            effect="plain"
            size="small">{{scope.row.isHot == 0 ? '正常' : '异常'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="isCough"
        header-align="center"
        align="center"
        label="是否咳嗽呼吸困难">
        <template slot-scope="scope">
          <el-tag
            :key="scope.row.isCough"
            :type="scope.row.isCough == 0 ? 'success' :  'danger'"
            effect="plain"
            size="small">{{scope.row.isCough == 0 ? '正常' : '异常'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="warmCorner"
        header-align="center"
        align="center"
        label="最近一周是否去过高风险地区">
        <template slot-scope="scope">
          <el-tag
            :key="scope.row.warmCorner"
            :type="scope.row.warmCorner == 0 ? 'success' :  'danger'"
            effect="plain"
            size="small">{{scope.row.warmCorner == 0 ? '未去过' : '去过'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="healthCode"
        header-align="center"
        align="center"
        label="健康码是否正常">
        <template slot-scope="scope">
          <el-tag
            :key="scope.row.healthCode"
            :type="scope.row.healthCode == 1 ? 'success' :  'danger'"
            effect="plain"
            size="small">{{scope.row.healthCode == 1 ? '正常' : '异常'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="punchLocation"
        header-align="center"
        align="center"
        label="打卡所在地">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="班主任操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.punchStatus == 0" type="text" size="small" @click="addOrUpdateHandle(scope.row.punchId)">手动打卡</el-button>
          <el-button v-if="scope.row.punchStatus == 1" type="text" size="small" @click="refusePunch(scope.row.punchId)">撤销打卡</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './punch-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          punchDate: '',
          username: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/headmaster/punch/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'punchDate': this.dataForm.punchDate,
            'nickName': this.dataForm.username
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      passPunch(id) {
        this.$http({
              url: this.$http.adornUrl(`/headmaster/punch/passPunch/${id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '通过打卡成功',
                  type: 'success'
                  });
              }
              this.getDataList();
            })
      },
      refusePunch(id) {
        this.$http({
              url: this.$http.adornUrl(`/headmaster/punch/refusePunch/${id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '驳回打卡成功',
                  type: 'success'
                  });
              }
              this.getDataList();
            })
      }

    }
  }
</script>
